<template>
  <a-table
    v-if="dataSource.length > 0"
    :dataSource="dataSource"
    :columns="columnsFilter"
    :pagination="query"
    @change="onChange"
    :loading="loading"
    :size="lineheight"
    :row-key="record => record.id"
    :row-selection="rowSelectionActive ? rowSelection : null"
    :filter="filtersAudit"
    force-render
  >
    <template #bodyCell="{ column = {}, record }">
      <template v-if="column.key === 'operationMsg'">
        <detail-button :data="record" />
      </template>
      <template v-if="column.key === 'createTime'">
        <span>
          {{
            record.createTime
              ? dayjs.utc(record.createTime).format('YYYY-MM-DD HH:mm:ss')
              : ''
          }}
        </span>
      </template>
      <template v-if="column.key === 'accessSuccess'">
        <span>
          <a-tag :color="record.accessSuccess === true ? 'green' : 'red'">
            {{ record.accessSuccess === true ? '成功' : '失败' }}
          </a-tag>
        </span>
      </template>
    </template>
  </a-table>
  <div
    v-else
    style="
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100%;
    "
  >
    <a-spin :spinning="loading">
      <a-empty
        :image="notData"
        :description="loading ? '加载中...' : '暂无数据'"
      />
    </a-spin>
  </div>
</template>
<script setup>
import { onMounted, computed, watch } from 'vue'
import ATable from '@/components/table.vue'
import detailButton from './table-content-detail.vue'

import notData from '@/assets/images/not-data.png'
import {
  useColumns,
  useQuery,
  useDataSourceManager,
  useDataSource,
  useLineheight,
  useLoading,
  useRowSelection,
  useRowSelectionActive,
  useFiltersAudit,
  useSortAudit,
} from './provide-context.js'
import dayjs from 'dayjs'
/**
 * 全局数据
 */
let columns = useColumns()
let dataSource = useDataSource()
let filtersAudit = useFiltersAudit()
let sortAudit = useSortAudit()
let query = useQuery()
let loading = useLoading()
let lineheight = useLineheight()
let { update } = useDataSourceManager()
/**
 * 表头
 */
let columnsFilter = computed(() => {
  // 给表格列添加过滤和排序

  columns.value.forEach(e => {
    // if (e.key === 'createTime') {
    //   e.sortOrder =
    //     sortAudit?.value?.columnKey === 'createTime' && sortAudit?.value?.order
    // } else if (e.key === 'accessSuccess') {
    //   e.filteredValue = filtersAudit?.value?.accessSuccess || null
    // } else if (e.key === 'userName') {
    //   e.filteredValue = filtersAudit?.value?.userName || null
    // } else if (e.key === 'accessDuration') {
    //   e.filteredValue = filtersAudit?.value?.accessDuration || null
    // }
    if (e.key === 'operationModule') {
      e.filteredValue = filtersAudit?.value?.operationModule || null
    }
  })

  return columns?.value?.filter(({ checked }) => checked) || []
})
/**
 * table组件分页、排序、筛选变化事件
 */
function onChange({ current, pageSize }, filters, sorter, { action }) {
  query.value.current = current
  query.value.pageSize = pageSize
  if (action !== 'paginate') {
    query.value.current = 1
    query.value.pageSize = 10
  }
  filtersAudit.value = filters
  sortAudit.value = sorter
  update()
}
//初始化调用
onMounted(() => {
  query.value.current = 1
  query.value.pageSize = 10
  update()
})
/**
 * 复选框
 */
let rowSelection = useRowSelection()
let rowSelectionActive = useRowSelectionActive()
</script>
